using System;
using System.Collections.Generic;
using System.Linq;
/*
    This file is part of Starlight Engine.

    Starlight Engine is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    Starlight Engine is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Starlight Engine.  If not, see <http://www.gnu.org/licenses/>.
*/

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using StarlightEngine;

namespace StarlightEngine
{
    #if ZUNE

    public enum Orientation
    {
        LandscapeLeft,
        /// <summary>
        /// The right side of the zune is on the bottom of the view
        /// </summary>
        LandscapeRight,
        NormalUpright,
        NormalUpsideDown,
        Flat,
        FlatUpsideDown
        
    }
    /// <summary>
    /// Controls input from the accelerometer.
    /// </summary>
    public class AccelerometerInput
    {
        public Orientation Orientation = Orientation.Flat;
        AccelerometerState state;        
        /// <summary>
        /// Gets the state of the accelerometer.
        /// </summary>
        /// <value>The state of the accelerometer.</value>
        internal AccelerometerState AccelerometerState
        {
            get { return state; }
        }

        /// <summary>
        /// Gets the acceleration.
        /// </summary>
        /// <value>The acceleration.</value>
        public Vector3 Acceleration
        {
            get { return state.Acceleration; }
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="AccelerometerInput"/> class.
        /// </summary>
        public AccelerometerInput()
        {
            state = Accelerometer.GetState();
            Logger.Write("Accelerometer Initialized.", 2);
        }


        /// <summary>
        /// Updates the accelerometer.
        /// </summary>
        /// <param name="time">The time.</param>
        public void Update(GameTime time)
        {
            state = Accelerometer.GetState();
            //Calculate what mode it is in.
            if (Acceleration.X <= -0.4f)
            {
                Orientation = Orientation.LandscapeLeft;
            }
            else if (Acceleration.X >= 0.4f)
            {
                Orientation = Orientation.LandscapeRight;
            }
            else if (Acceleration.Y <= -0.4f) 
            {
                Orientation = Orientation.NormalUpright;
            }
            else if (Acceleration.Y >= 0.4f)
                Orientation = Orientation.NormalUpsideDown;
            else if (Acceleration.Z <= -0.4f)
            {
                Orientation = Orientation.Flat;
            }
            else if (Acceleration.Z >= 0.4f)
                Orientation = Orientation.FlatUpsideDown;
            else
            {
                //throw new Exception("Some sort of orientation has been missed.\nPlease report it with a description and the following value.\n" + Acceleration.ToString() + ".\nThanks.");
                Orientation = Orientation.Flat;
            }
           
        }
    }
#endif
}